// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store'
import VueScroller from 'vue-scroller'
Vue.config.productionTip = false
Vue.use(VueScroller)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
/**
* 获得需签合同的公共函数，先验证有没有token，再验证vuex里的contrats有没缓存，下拉刷新每次都会调用
* @param {boolen} refresh 是否为下拉刷新
*/
Vue.prototype.getContract = function (refresh) {
  let token = localStorage.token
  if (token) {
    if (this.$store.state.contracts.length !== 0 && !refresh) {
      this.contracts = this.$store.state.contracts
      store.commit('setShowLoad', false)
      return
    }
    this.$store.dispatch('getContract').then(res => {
      let result = res.data
      if (result.Code === 510) {
        this.contracts = result.Contract
      } else if (result.Code === 405 || result.Code === 403) {
        this.setToast({
          show: true,
          type: '',
          text: result.Message
        })
        this.setLogin(true)
        this.setContract('')
      } else {
        this.setToast({
          show: true,
          type: '',
          text: result.Message
        })
      }
    })
  }
}
